package samplegameplugin;

import gamefw.framework.Coordinate;
import gamefw.gameplugin.GameBoard;
import gamefw.gameplugin.GameEntity;

import java.util.ArrayList;

public class SampleGameBoard extends GameBoard {
	
	private static final int NUM_ROWS = 3;
	private static final int NUM_COLS = 3;
	
	private SampleGameEntity[][] entities;
	
	private String resourceType;
	
	public SampleGameBoard() {
		entities = new SampleGameEntity[NUM_ROWS][NUM_COLS];
		for (int row = 0; row < NUM_ROWS; ++row) {
			for (int col = 0; col < NUM_COLS; ++col) {
				entities[row][col] = null;
			}
		}
	}

	@Override
	public String getVisualResource(String resourceType) {
		// Remember which resource type was used.
		this.resourceType = resourceType;
		String resource = null;
		
		if (resourceType.equals("txt")) {
			resource = "SampleBoard.txt";
		} else if (resourceType.equals("png")) {
			resource = "SampleBoard.png";
		} else {
			this.resourceType = null;
			return null;
		}
		
		return this.getClass().getResource(resource).getFile();
	}

	@Override
	public Coordinate getLogicalPosition(Coordinate physicalPosition) {
		if (this.resourceType.equals("txt")) {
			return new Coordinate(physicalPosition);
		} else if (this.resourceType.equals("png")) {
			int newX = physicalPosition.getX() / 100;
			int newY = (physicalPosition.getY() - 50) / 100;
			return new Coordinate(newX, newY);
		} else {
			return null;
		}
	}

	@Override
	public Coordinate getPhysicalPosition(Coordinate logicalPosition) {
		if (this.resourceType.equals("txt")) {
			
			int newX = 4 + (logicalPosition.getX() * 4);
			int newY = 3 + (logicalPosition.getY() * 2);
			return new Coordinate(newX, newY);
			
		} else if (this.resourceType.equals("png")) {
			
			int newX = logicalPosition.getX() * 100;
			int newY = (logicalPosition.getY() * 100 + 50);
			return new Coordinate(newX, newY);
			
		} else {
			
			return null;
			
		}
	}

	@Override
	public void initGameBoard(GameEntity[] entities) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public int getRowsCount() {
		return NUM_ROWS;
	}
	
	@Override
	public int getColsCount() {
		return NUM_COLS;
	}

	@Override
	public GameEntity getEntity(Coordinate position) {
		if (position.getX() < 0 || position.getX() >= NUM_COLS ||
			position.getY() < 0 || position.getY() >= NUM_ROWS) {
			throw new IndexOutOfBoundsException();
		}
		
		return this.entities[position.getY()][position.getX()];
	}

	@Override
	public void setEntity(Coordinate position, GameEntity entity) {
		if (position.getX() < 0 || position.getX() >= NUM_COLS ||
			position.getY() < 0 || position.getY() >= NUM_ROWS) {
			throw new IndexOutOfBoundsException();
		}
		
		if (!(entity instanceof SampleGameEntity)) {
			throw new IllegalArgumentException();
		}
		
		entity.setPosition(position);
		this.entities[position.getY()][position.getX()] = (SampleGameEntity)entity;
	}

	@Override
	public void display() {
		Coordinate coordinate = new Coordinate(0, 0);
		if (this.resourceType.equals("png")) {
			coordinate.setY(coordinate.getY() + 50);
		}
		
		getVisualResource().draw(coordinate);
		
		for (GameEntity entity : getEntities()) {
			Coordinate physPosition = getPhysicalPosition(entity.getPosition());
			entity.getVisualResource().draw(physPosition);
		}
	}

	@Override
	public Coordinate[] getEmptyPositions() {
		ArrayList<Coordinate> positions = new ArrayList<Coordinate>();
		
		for (int row = 0; row < NUM_ROWS; ++row) {
			for (int col = 0; col < NUM_COLS; ++col) {
				if (this.entities[row][col] == null) {
					positions.add(new Coordinate(col, row));
				}
			}
		}
		
		return positions.toArray(new Coordinate[]{});
	}

	@Override
	public GameEntity[] getEntities() {
		ArrayList<GameEntity> entities = new ArrayList<GameEntity>();
		
		for (int row = 0; row < NUM_ROWS; ++row) {
			for (int col = 0; col < NUM_COLS; ++col) {
				if (this.entities[row][col] != null) {
					entities.add(this.entities[row][col]);
				}
			}
		}
		
		return entities.toArray(new GameEntity[]{});
	}

}
